/**查找分页控件 -> 请求下一页 -> 增删元素 -> 加载在页面 */
async function loadNextPage() {
  let [lastNrPage] = [...document.getElementsByClassName('link')].slice(-1)
  let url = lastNrPage.querySelector('a:nth-child(3)').href

  const response = await fetch(url)
  const decoder = new TextDecoder('utf8')
  const html = decoder.decode(await response.arrayBuffer())
  let div = document.createElement('div')
  div.innerHTML = html

  let alink = document.createElement('a')
  alink.href = url
  alink.innerText = '本页'
  div.querySelector('.link').appendChild(alink)

  div.querySelector('#chapter').style.borderLeftWidth = '0'
  div.querySelector('#chapter').style.borderRrightWidth = '0'
  div.querySelector('#chapter').style.borderBottomWidth = '0'

  $chromePlugin.cleanPage(div, ['#header', '.crumbs', '.bookset', '.clr', '.copyright', '#popbg', '#footer'])

  let [lastContent] = [...document.getElementsByClassName('article')].slice(-1)
  lastContent.parentNode.appendChild(div)
  return { div, url }
}

/**从页面获取书名 */
function getBookName() {
  if (document.querySelector('#info h1')) {
    return document.querySelector('#info h1').innerText
  } 
  if (document.querySelector('.con_top>a:nth-of-type(3)')) {
    return document.querySelector('.con_top>a:nth-of-type(3)').innerText
  }
}

/**_addObserver传入targetSelector、chapterTitleSelector*/
function addObserver(dom = document, url = location.href) {
  _addObserver('.content', '.bookName h1', dom, url)
}
